import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/theme.dart';

import '../../../constants.dart';
import '../../../widget/tag.dart';
import '../OrderStatusHandler.dart';
import 'logic.dart';

class OrderPaidDetailPage extends StatelessWidget {
  OrderPaidDetailPage({Key? key}) : super(key: key);

  final logic = Get.find<OrderPaidDetailLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('付款详情'),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      body: ListView(
        padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
        children: [
          SizedBox(height: kSpaceSmall),
          _buildMoneyInfo(),
          SizedBox(height: kSpaceSmall),
          _buildPaymentList(),
          SizedBox(height: kSpaceSmall),
          _buildOrderInfoList(),
          SizedBox(height: kSpaceSmall),
          _buildCaseInfoList(),
          SizedBox(height: kSpaceMedium),
          Obx(() => logic.orderDetail.value!.orderStatus! == 0
              ? _buildCancelButton()
              : Container()),
          SizedBox(height: kSpaceMedium),
        ],
      ),
    );
  }

  Widget _buildCancelDialog(BuildContext context) {
    return AlertDialog(
      title: const Text('取消订单'),
      content: const Text('确定取消当前订单吗？'),
      actions: [
        TextButton(
          onPressed: () => Get.back(),
          child: const Text('取消'),
        ),
        TextButton(
          onPressed: () {
            logic.cancelOrder();
            Get.back();
          },
          child: const Text('确定'),
        ),
      ],
    );
  }

  Widget _buildCancelButton() {
    return Container(
      height: 50.w,
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      child: FilledButton(
        style: ButtonStyle(
          backgroundColor: WidgetStateProperty.all(Colors.grey[350]),
        ),
        onPressed: () =>
            showDialog(context: Get.context!, builder: _buildCancelDialog),
        child: const Text('取消订单'),
      ),
    );
  }

  Widget _buildModuleTitle(String title) {
    return Text(
      title,
      style: TextStyle(
        fontSize: kFontSizeMedium,
        color: AppColor.colorFontBlack,
        fontWeight: AppFont.fontMedium,
      ),
    );
  }

  Widget _valueText(String? value) {
    return SizedBox(
      child: Text(
        textAlign: TextAlign.right,
        value ?? '',
        style: TextStyle(
          fontSize: kFontSizeSmall,
          color: AppColor.colorFontBlack,
        ),
      ),
    );
  }

  Widget _labelText(String value) {
    return SizedBox(
      width: 100.w,
      child: Text(
        value,
        style: TextStyle(
          fontSize: kFontSizeSmall,
          color: AppColor.colorFontBlack,
        ),
      ),
    );
  }

  Widget _buildRowItem(String label, String value) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        _labelText(label),
        _valueText(value),
      ],
    );
  }

  Widget _buildPaymentList() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: kSpaceMedium,
            vertical: 14.w,
          ),
          child: _buildModuleTitle('付款对象'),
        ),
        Container(
          width: double.maxFinite,
          height: 0.5.w,
          color: AppColor.colorLineGrey,
        ),
        SizedBox(height: kSpaceMini),
        ...List.generate(
          logic.paymentInfo.keys.length,
          (index) => Padding(
            padding: EdgeInsets.symmetric(
              horizontal: kSpaceMedium,
              vertical: kSpaceSmall,
            ),
            child: _buildRowItem(
              logic.paymentInfo.keys.toList()[index],
              logic.paymentInfo.values.toList()[index],
            ),
          ),
        ),
      ]),
    );
  }

  Widget _buildOrderInfoList() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.symmetric(
              horizontal: kSpaceMedium,
              vertical: 14.w,
            ),
            child: _buildModuleTitle('订单详情'),
          ),
          Container(
            width: double.maxFinite,
            height: 0.5.w,
            color: AppColor.colorLineGrey,
          ),
          SizedBox(height: kSpaceMini),
          ...List.generate(
            logic.orderInfo.keys.length,
            (index) => Padding(
              padding: EdgeInsets.symmetric(
                horizontal: kSpaceMedium,
                vertical: kSpaceSmall,
              ),
              child: _buildRowItem(
                logic.orderInfo.keys.toList()[index],
                logic.orderInfo.values.toList()[index],
              ),
            ),
          ),
          SizedBox(height: kSpaceMini),
        ],
      ),
    );
  }

  Widget _buildCaseInfoList() {
    return Ink(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.symmetric(
              horizontal: kSpaceMedium,
              vertical: 14.w,
            ),
            child: _buildModuleTitle('关联案件'),
          ),
          ...List.generate(
            logic.caseList.length,
            (index) => Padding(
              padding: EdgeInsets.only(
                left: kSpaceMedium,
                right: kSpaceMedium,
                bottom: kSpaceSmall,
              ),
              child: InkWell(
                borderRadius: BorderRadius.circular(kRadiusMedium),
                onTap: () => logic.onClickCase(index),
                child: Tag(
                  text: logic.caseList[index].caseName ?? '',
                  textColor: AppColor.colorFontBlack,
                  textSize: kFontSizeMini,
                  borderColor: AppColor.colorFontGrey,
                  borderRadius: kRadiusMedium,
                  padding: EdgeInsets.symmetric(
                    horizontal: kSpaceSmall,
                    vertical: 14.w,
                  ),
                  rightIcon: Icon(
                    Icons.arrow_forward_ios,
                    size: 13.w,
                    color: AppColor.colorFontGrey,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildMoneyInfo() {
    return Container(
      padding: EdgeInsets.symmetric(
        horizontal: kSpaceMedium,
        vertical: kSpaceSmall,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            payOrderStatus[logic.orderDetail.value!.orderStatus] ?? '',
            style: TextStyle(
              fontSize: kFontSizeLarge,
              color: AppColor.colorFontBlack,
              fontWeight: AppFont.fontRegular,
            ),
          ),
          SizedBox(height: kSpaceTiny),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                '付款金额',
                style: TextStyle(
                  fontSize: kFontSizeMini,
                  fontWeight: AppFont.fontRegular,
                  color: AppColor.colorFontGrey,
                ),
              ),
              SizedBox(width: kSpaceMini),
              Text(
                '¥${logic.orderDetail.value!.payAmount}',
                style: TextStyle(
                  fontSize: kFontSizeMedium,
                  fontWeight: AppFont.fontMedium,
                  color: AppColor.colorFontBlack,
                ),
              ),
              SizedBox(width: kSpaceBig),
              Text(
                '应扣金额',
                style: TextStyle(
                  fontSize: kFontSizeMini,
                  fontWeight: AppFont.fontRegular,
                  color: AppColor.colorFontGrey,
                ),
              ),
              SizedBox(width: kSpaceMini),
              Text(
                '¥${logic.orderDetail.value!.orderAmount}',
                style: TextStyle(
                  fontSize: kFontSizeMedium,
                  fontWeight: AppFont.fontMedium,
                  color: AppColor.colorFontBlack,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
